<?php

namespace App\Http\Controllers\Api;

use App\Activity;
use App\ActivitySignup;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Carbon\Carbon;

class ActivityController extends Controller
{
    public function index()
    {
        $acti = Activity::all();
        $filter = collect();
        foreach ($acti as $key ) {
            if($key->status == 1)
                $filter->push($key);
        }
        return [
            'status' => 'success',
            'data_size' => $filter->count(),
            'data' => $filter
        ];
    }

    public function detail($id)
    {
        return [
            'status' => 'success',
            'data' => Activity::find($id)
        ];
    }

    public function signuplist($id)
    {
        $signlist = Activity::find($id)->signlist;
        foreach ($signlist as $item ) {
            $user = User::find($item->user_id);
            $item->activity_title = Activity::find($item->activity_id)->title;
            $item->user = [
                'username'=> $user->username,
                'age' => Carbon::parse($user->birthdate)->diffInYears(),
                'gender' => $user->gender ? 'man' : 'woman'
            ];
        }
        return [
            'status' => 'success',
            'data_size' => $signlist->count(),
            'data' => $signlist
        ];
    }

    public function signup($id)
    {
        $signup = ActivitySignup::create([
            'status' => 0,
            'signup_time' => now(),
            'user_id' => 1,
            'activity_id' => $id
        ]);

        return [
            'status' => 'success',
            'data' => [
                'activity_signup_id' => $signup->id,
                'signup_time' => $signup->signup_time
            ]
        ];
    }
}
